﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace clock
{
    public class AlarmClock
    {
        // 定义 Tick 和 Alarm 事件
        public event EventHandler Tick; // Tick 事件
        public event EventHandler Alarm; // Alarm 事件

        // 当前时间和响铃时间
        public DateTime CurrentTime { get; private set; }
        public DateTime AlarmTime { get; set; }

        // 构造函数，初始化当前时间和响铃时间
        public AlarmClock(DateTime alarmTime)
        {
            CurrentTime = DateTime.Now; // 当前时间
            AlarmTime = alarmTime; // 响铃时间
        }
        public void Start()
        {
            Console.WriteLine("闹钟启动");
            while (CurrentTime < AlarmTime)
            {
                Thread.Sleep(1000); // 每秒更新一次时间
                CurrentTime = CurrentTime.AddSeconds(1); // 时间增加 1 秒
                OnTick();
                if (CurrentTime >= AlarmTime)
                {
                    OnAlarm();
                }
            }
        }
        protected virtual void OnTick()
        {
            Tick?.Invoke(this, EventArgs.Empty);
        }
        protected virtual void OnAlarm()
        {
            Alarm?.Invoke(this, EventArgs.Empty);
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            DateTime alarmTime = DateTime.Now.AddSeconds(10);
            AlarmClock alarmClock = new AlarmClock(alarmTime);
            alarmClock.Tick += (sender, e) =>
            {
                Console.WriteLine($"当前时间 {DateTime.Now.ToString("HH:mm:ss")}");
            };
            alarmClock.Alarm += (sender, e) =>
            {
                Console.WriteLine("响铃时间到！");
            };
            alarmClock.Start();
        }
    }
}
